package com.zjh.photoshopstudy.app.collection;

import android.content.Intent;

import com.zjh.photoshopstudy.Model.VideoEntity;
import com.zjh.photoshopstudy.Utils.SharePreferencesUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class CollectPresent {
    private CollectionActivity mContext;
    private CollectionHttp mHttp;
    boolean isCouse;

    public CollectPresent(CollectionActivity mContext, boolean isCouse) {
        this.mContext = mContext;
        mHttp = new CollectionHttp();
        this.isCouse = isCouse;
    }

    public void deleteCollect(List<VideoEntity> collectionList) {
        int uid = (int) SharePreferencesUtils.getLoginUser(mContext).getId();
        Set<Integer> collectionIdSet = new HashSet();
        for (VideoEntity e : collectionList) {
            collectionIdSet.add((int) e.getId());
        }
        mHttp.deleteCollect(new OnDeleteCollect(), collectionIdSet, uid);
    }

    public void getCollectList() {
        int uid = (int) SharePreferencesUtils.getLoginUser(mContext).getId();
        mHttp.getCollectList(new OnGetCollectList(), uid);
    }

    public void getCourseList() {
        int uid = (int) SharePreferencesUtils.getLoginUser(mContext).getId();
        mHttp.getCourseList(new OnGetCollectList(), uid);
    }

    public void deleteCourse(List<VideoEntity> collectionList) {
        int uid = (int) SharePreferencesUtils.getLoginUser(mContext).getId();
        Set<Integer> collectionIdSet = new HashSet();
        for (VideoEntity e : collectionList) {
            collectionIdSet.add((int) e.getId());
        }
        mHttp.deleteCourse(new OnDeleteCollect(), collectionIdSet, uid);
    }

    class OnDeleteCollect {
        void onDeleteCollectSuccess(Set<Integer> collectionIdSet, int uid) {
            if (!isCouse) {
                Set<Integer> set = new HashSet<>();
                for (int e : collectionIdSet) {
                    set.add(e);
                }
                SharePreferencesUtils.deleteCollection(mContext, uid, set);
            }
            mContext.onDeleteCollectSuccess();
        }

        void onDeleteCollectFail() {
            mContext.onDeleteCollectFail();
        }
    }

    class OnGetCollectList {
        void OnGetCollectListSuccess(List<VideoEntity> collectionList, int uid) {
            if (!isCouse) {
                SharePreferencesUtils.clearCollection(mContext, uid);
                for (VideoEntity e : collectionList) {
                    SharePreferencesUtils.saveCollection(mContext, uid, (int) e.getId());
                }
            }
            mContext.OnGetCollectListSuccess(collectionList);
        }

        void OnGetCollectListFail() {
            mContext.OnGetCollectListFail();
        }
    }
}
